package org.springrain.selenium.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springrain.frame.common.SessionUser;
import org.springrain.frame.entity.IBaseEntity;
import org.springrain.frame.util.Finder;
import org.springrain.frame.util.Page;
import org.springrain.selenium.entity.Htmlcaseexec;
import org.springrain.selenium.entity.Htmloperation;
import org.springrain.selenium.entity.Htmlparameter;
import org.springrain.selenium.service.IHtmlSeleniumService;
import org.springrain.selenium.service.IHtmlcaseexecService;
import org.springrain.system.service.BaseSpringrainServiceImpl;
import org.springrain.system.service.ITableindexService;


/**
 * TODO 在此加入类描述
 * @copyright {@link weicms.net}
 * @author springrain<Auto generate>
 * @version  2018-06-02 21:44:30
 * @see org.springrain.selenium.service.impl.Htmlcaseexec
 */
@Service("htmlcaseexecService")
public class HtmlcaseexecServiceImpl extends BaseSpringrainServiceImpl implements IHtmlcaseexecService {

    @Resource
    private IHtmlSeleniumService htmlSeleniumService;
    

    @Resource
    private ITableindexService tableindexService;
    @Override
	public String  save(Object entity ) throws Exception{
	      Htmlcaseexec htmlcaseexec=(Htmlcaseexec) entity;
	       return super.save(htmlcaseexec).toString();
	}

    @Override
	public String  saveorupdate(Object entity ) throws Exception{
	      Htmlcaseexec htmlcaseexec=(Htmlcaseexec) entity;
		 return super.saveorupdate(htmlcaseexec).toString();
	}
	
	@Override
    public Integer update(IBaseEntity entity ) throws Exception{
	 Htmlcaseexec htmlcaseexec=(Htmlcaseexec) entity;
	return super.update(htmlcaseexec);
    }
    @Override
	public Htmlcaseexec findHtmlcaseexecById(Object id) throws Exception{
	 return super.findById(id,Htmlcaseexec.class);
	}
	
/**
 * 列表查询,每个service都会重载,要把sql语句封装到service中,Finder只是最后的方案
 * @param finder
 * @param page
 * @param clazz
 * @param o
 * @return
 * @throws Exception
 */
        @Override
    public <T> List<T> findListDataByFinder(Finder finder, Page page, Class<T> clazz,
			Object o) throws Exception{
			 return super.findListDataByFinder(finder,page,clazz,o);
			}
	/**
	 * 根据查询列表的宏,导出Excel
	 * @param finder 为空则只查询 clazz表
	 * @param ftlurl 类表的模版宏
	 * @param page 分页对象
	 * @param clazz 要查询的对象
	 * @param o  querybean
	 * @return
	 * @throws Exception
	 */
		@Override
	public <T> File findDataExportExcel(Finder finder,String ftlurl, Page page,
			Class<T> clazz, Object o)
			throws Exception {
			 return super.findDataExportExcel(finder,ftlurl,page,clazz,o);
		}

    @Override
    public String updateExecCaseByBusinessId( Htmlcaseexec htmlexec) throws Exception {
       
        String businessId=htmlexec.getBusinessId();
        List<Htmlparameter> parameters=htmlexec.getParameters();
        Htmlcaseexec exec = new Htmlcaseexec();

        String execId = tableindexService.updateNewId(Htmlcaseexec.class);

        exec.setId(execId);
        exec.setStartDate(new Date());
        exec.setBusinessId(businessId);
        exec.setBusinessName(htmlSeleniumService.findOperationCollectionNameById(businessId));
        exec.setUserId(SessionUser.getUserId());
        
        
        if(CollectionUtils.isNotEmpty(parameters)) {
            for(Htmlparameter hp:parameters) {
                hp.setExecId(execId);
                hp.setId(execId+"-"+hp.getOperationId()+"-"+hp.getSortno());
            }
            
            super.save(parameters);
            
        }
        
        
        
        
        List<Htmloperation> list=new ArrayList<>();
        String errorMsg=htmlSeleniumService.updateExecOperationBybusinessId(businessId, list);

        
        if(StringUtils.isNotBlank(errorMsg)) {
            exec.setEndDate(new Date());
            super.save(exec);
            return errorMsg;
        }
        
        
        htmlSeleniumService.updateExecOperationList(list,businessId,execId);
        
        exec.setEndDate(new Date());
        super.save(exec);
        
        return null;
    }

}
